﻿<%@ WebHandler Language="C#" Class="LogHandlerService" %>

using System;
using System.Web;

using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using XJX.BusinessLayer;

using XJX.Page;

public class LogHandlerService : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";

        string response = "";

        String methodName = context.Request["method"];
        Type type = this.GetType();
        MethodInfo method = type.GetMethod(methodName);


        if (method == null)
        {
            response = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "调用的方法不存在！").ToJson(); return;
        }

        try
        {
            BeforeInvoke(methodName);
            response = method.Invoke(this, null).ToString();
        }
        catch (Exception ex)
        {
            Hashtable result = new Hashtable();
            result["error"] = -1;
            result["message"] = ex.Message;
            result["stackTrace"] = ex.StackTrace;
            String json = XJX.BusinessLayer.JSON.Encode(result);
            context.Response.Clear();
            context.Response.Write(json);
        }
        finally
        {
            AfterInvoke(methodName);
            HttpContext.Current.Response.Write(response);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //权限管理
    protected void BeforeInvoke(String methodName)
    {
        //Hashtable user = GetUser();
        //if (user.role == "admin" && methodName == "remove") throw .      
    }
    //日志管理
    protected void AfterInvoke(String methodName)
    {

    }


    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================

    //退出登录
    public string ExitLogStatus()
    {
        HttpContext.Current.Session["FamilyInfo"]=null;

        return new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "您已经退出登录，网页将转到登录页!").ToJson();
    }
    
    //验证用户登录
    public string Verification_User_Log()
    {
        string mobile = HttpContext.Current.Request.Form["phoneNumber"];//客户端网页元素必须使用名称表示，不能使用ID表示。例如：name='bname'

        string pwd = HttpContext.Current.Request.Form["inputPassword"];

        //下面开始调用验证,ReturnFieldValue为返回的用户UID值。
        string ReturnFieldValue;
        int ReturnValue = XJX.Page.Log.Verification_User_Log(mobile, pwd, out ReturnFieldValue);

        string result = "";

        //返回值给前端处理
        switch (ReturnValue)
        {
            case 0:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "手机号或密码错!").ToJson();
                break;
            case 1:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Restrict, "当前手机号被限制登录!").ToJson();
                break;
            case 2:
                //为页面Session赋家庭信息类值
                //若要在ashx文件中使用Session，则必须继承接口System.Web.SessionState.IRequiresSessionState，参本文件15行。
                if (HttpContext.Current.Session["FamilyInfo"] == null)
                {
                    FamilyInfo fi = new FamilyInfo();
                    fi = XJX.Page.Family.Get_One_Family_Info_From_UID(ReturnFieldValue);
                    HttpContext.Current.Session["FamilyInfo"] = fi;//保持用户信息

                    ParamInfo pi = new ParamInfo();
                    pi = XJX.Page.Other.Get_ALL_Param_For_Class();
                    HttpContext.Current.Session["ParamInfo"] = pi;//保持参数信息

                    //XJX.Page.Log.Add_New_Log(fi.UID);//记录登录事件，在存储过程中已经有此动作。

                }

                //转向移动到家庭管理页面，每页需要先验证上面的Session值的内容
                //HttpContext.Current.Response.Redirect("~/Family/AllBookList.aspx",false);
                //HttpContext.Current.Response.Write("location.href='~/Family/AllBookList.aspx';");
                //HttpContext.Current.Server.Execute("AllBookList.aspx");


                //返回前端处理
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "验证成功!").ToJson();
                break;
            default:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.OtherError, "未知原因导致登录失败!").ToJson();
                break;
        }
        return result;
    }

    //修改家庭登录密码
    public string Modify_One_Family_PWD()
    {
        string result = "";

        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];

        string UID = fi.UID;
        string PWD = fi.PWD;

        string OldPassWord = HttpContext.Current.Request["OldPassWord"];

        string NewPassWord1 = HttpContext.Current.Request["NewPassWord1"];
        string NewPassWord2 = HttpContext.Current.Request["NewPassWord2"];

        if (OldPassWord != PWD)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "原有密码不正确，修改未成功!").ToJson();
        }
        else
        {
            if (NewPassWord1 != NewPassWord2)
            {
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "两个新密码不一致，修改未成功!").ToJson();
            }
            else
            {
                if (XJX.Page.Log.Modify_One_Family_PWD(UID, NewPassWord1))
                {
                    result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "密码修改成功!").ToJson();
                }
                else
                {
                    result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "密码修改失败，请联系管理员!").ToJson();
                }
            }
        }

        return result;
    }
    
    public string Add_New_Log()
    {
        string result = "";

        //在业务层已经处理。
        return result;
    }

}